# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
#import webrepl
#webrepl.start()
import socket
import network
from machine import Pin
import time

# 控制1个电机
p13 = Pin(13, Pin.OUT)
p12 = Pin(12, Pin.OUT)
# 控制1个电机
p14 = Pin(14, Pin.OUT)
p27 = Pin(27, Pin.OUT)
# 控制1个电机
p15 = Pin(15, Pin.OUT)
p2 = Pin(2, Pin.OUT)
# 控制1个电机
p4 = Pin(4, Pin.OUT)
p16 = Pin(16, Pin.OUT)


ap = network.WLAN(network.AP_IF)
ap.active(True)   
ap.config(essid='aph123',authmode=3,password='00000000')

def move_up():
    p13.value(1)
    p12.value(0)
    p14.value(1)
    p27.value(0)
    p15.value(1)
    p2.value(0)
    p4.value(1)
    p16.value(0)
    time.sleep(1)
def move_down():
    p13.value(0)
    p12.value(1)
    p14.value(0)
    p27.value(1)
    p15.value(0)
    p2.value(1)
    p4.value(0)
    p16.value(1)
    time.sleep(1)

def move_left():
    p13.value(1)
    p12.value(0)
    p14.value(0)
    p27.value(1)
    p15.value(0)
    p2.value(1)
    p4.value(1)
    p16.value(0)
    time.sleep(1)
def move_right():
    p13.value(0)
    p12.value(1)
    p14.value(1)
    p27.value(0)
    p15.value(1)
    p2.value(0)
    p4.value(0)
    p16.value(1)
    time.sleep(1)

def stop():
    p13.value(0)
    p12.value(0)
    p14.value(0)
    p27.value(0)
    p15.value(0)
    p2.value(0)
    p4.value(0)
    p16.value(0)

    
udp_sovket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udp_sovket.bind(("",8007))  
while True:
    data = udp_sovket.recvfrom(1024)
    recv = int(data[0])
    if recv == 1:
        move_up()
        stop()
    elif recv == 2:
        move_down()
        stop()
    elif recv == 3:
        move_left()
        stop()
    elif recv == 4:
        move_right()
        stop()



